package Management.fileIO;

import java.io.*;  

/**
* @version Excalibur 0.1.0
* @author zmmbreeze
*/
public class MyFileReader {
   /**
    * 构造方法 
    * @param filename
    */
	public MyFileReader (String filename) 
	{
		try
		{
			File f= new File(filename);
			if(!f.exists()) {
				f.createNewFile();
				f.setReadOnly();
			}
		}
		catch (Exception e){}
	}

	/**
	 * 获得所有每一行的内容
	 * @param  filename
	 * @return String的数组
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
    public String[] readl(String filename) throws FileNotFoundException,IOException
    {
        int a;
        MyFileReader r = new MyFileReader(filename);
        FileReader fr = new FileReader(filename);
        BufferedReader br = new BufferedReader(fr);
        a = r.getline(filename);
        String[] feedadd = new String[a];
        FileReader fr2 = new FileReader(filename);
        BufferedReader br2 = new BufferedReader(fr2);
        for(int i = 0; i < a ; i++){
        	feedadd[i] = br2.readLine();
        }
        fr.close();
        br.close();
        fr2.close();
        br2.close();
        return feedadd;
    }

    /**
     * 添加一行代码
     * @param filename
     * @param feedAdd
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void append(String filename,String feedAdd)throws FileNotFoundException, IOException
    {
    	MyFileReader r = new MyFileReader(filename);
        int length = r.getline(filename);
        String[] a = r.readl(filename);
        String[] b = new String[length+1];
        for(int i = 0; i < length; i++){
              b[i] = a[i];              
        }
        b[length] = feedAdd;
        try {  
             FileWriter fw = new FileWriter(filename,false);  
             PrintWriter pw=new PrintWriter(fw);  
             for(int j = 0; j < length + 1 ; j++){
                      pw.println(b[j]);
             }
             pw.close () ;  
             fw.close () ;  
         } catch (IOException e) {  
             e.printStackTrace();  
         }            
    }

    /**
     * 获得文件内的行数
     * @param filename
     * @return
     * @throws IOException
     */
    public int getline(String filename) throws IOException
    {
        int i = 0;
        FileReader fr = new FileReader(filename);
        BufferedReader br = new BufferedReader(fr);
        String s = null;
        do
        {
	        s = br.readLine();
	        if((s == null)||(s.trim().length() == 0)){break;}
	        i++;
        }while(true);
        return i;               
    }

    /**
     * 删除指定的行的文件
     * @param filename
     * @param d
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void delete(String filename,int d) throws FileNotFoundException, IOException {
    	MyFileReader r = new MyFileReader(filename);
        int length = r.getline(filename);
        String[] a = new String[length];
        a = r.readl(filename);
        if(length == 0){ System.exit(1); }
        String[] b = new String[length];
        for(int i = 0,j = 0; i < length; i++,j++){
        	if(i == d){
        		i++;
        		if(i == length) 
        			break;
        		b[j] = a[i];		
        	}
        	else{
        		b[j] = a[i];
            }              
        }
        b[length - 1] = "";
        try {  
             FileWriter fw = new FileWriter(filename,false);  
             PrintWriter pw=new PrintWriter(fw);  
             for(int j = 0; j < length ; j++){
                      pw.println(b[j]);
             }
             pw.close () ;  
             fw.close () ;  
        } catch (IOException e) {  
             e.printStackTrace();  
        }            
    }

}
